<?php
	function createAcc($user, $pass, $money, $db_con)
	{ 
		if(!empty($user) && !empty($pass))
		{
			$userLen = strlen($user);
			$passLen = strlen($pass);
			$pass = md5($pass);
			
			$checkQuery = "SELECT username FROM user WHERE username = '$user' ";
			$checkResult = mysqli_query($db_con, $checkQuery) or die('Error: ' .mysqli_error($db_con));
			
			if($userLen < 4 || $userLen > 12)
			{
				$_SESSION['error'] = 'Username must be between 4 or 12 characters!';
			} elseif ($passLen < 6)
			{
				$_SESSION['error'] = 'Password must be longer than 6 characters!';
			} elseif (mysqli_num_rows($checkResult) == 1)
			{
				$_SESSION['error'] = 'Username exists! Please select another.';
			} else 
			{
				$addQuery = "INSERT INTO user(`username`, `password`, `money`) VALUES ('$user','$pass', '$money')";
				$addResult = mysqli_query($db_con, $addQuery) or die('Error: ' .mysqli_error($db_con));
				
				if($addResult)
				{
					return true;
				}
			}
		}
		return false; 
	}

	function isLogged ()
	{
		if(isset($_SESSION['loggedin']) && isset($_SESSION['username']))
		{
			return true;
		} else 
			return false; 
	}
	
	function logout()
	{
		unset($_SESSION['loggedin']);
		unset($_SESSION['username']);
		
		return true;
	}
	
	function validateUser($user, $pass, $db_con)
	{
		$pass = md5($pass);
		$validateQuery = "SELECT username FROM user WHERE username = '$user' 
							AND password = '$pass'";
		$validateResult = mysqli_query($db_con, $validateQuery) or die("Error: " .mysqli_error($db_con));
		
		if(mysqli_num_rows($validateResult) == 1)
		{
			$row = mysqli_fetch_assoc($validateResult);
			$_SESSION['username'] = $row['username'];
			$_SESSION['loggedin'] = true; 
			
			return true;
		}
		
		return false; 
	}
?>